/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.apiserver.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.openislands.oi.apiserver.manager.NodeManager;
import org.openislands.oi.config.idgenerator.NodeIdGenerator;
import org.openislands.oi.pojo.base.BeanBuilder;
import org.openislands.oi.pojo.dto.NodeDTO;
import org.openislands.oi.pojo.dto.NodePageDTO;
import org.openislands.oi.pojo.dto.RouteDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class NodeService {
    @Resource(type = NodeManager.class)
    private NodeManager nodeManager;

    @Resource(type = NodeIdGenerator.class)
    private NodeIdGenerator nodeIdGenerator;

    public Boolean addNode(NodeDTO nodeDTO) {
        if (StringUtils.isBlank(nodeDTO.getNodeId())) {
            nodeDTO.setNodeId(nodeIdGenerator.getNodeId());
        }
        for (RouteDTO routeDTO : nodeDTO.getRouteDTOList()) {
            routeDTO.setNodeId(nodeDTO.getNodeId());
        }
        return nodeManager.addNode(nodeDTO);
    }

    public Boolean deleteNode(String nodeId) {
        final NodeDTO nodeDTO = BeanBuilder.create(new NodeDTO()).set(NodeDTO::setNodeId, nodeId).build();
        return nodeManager.deleteNode(nodeDTO);
    }

    public Boolean updateNode(NodeDTO nodeDTO) {
        return nodeManager.updateNodeInfo(nodeDTO);
    }

    public Boolean resetNode(NodeDTO nodeDTO) {
        return nodeManager.resetNode(nodeDTO);
    }

    public NodeDTO queryByNodeId(String nodeId) {
        return nodeManager.queryNodeDetails(nodeId);
    }

    public Page<NodeDTO> queryPage(NodePageDTO pageDTO) {
        return nodeManager.queryPage(pageDTO);
    }
}
